<?php
class memberApi extends core 
{
	public function loginAction($arg)
	{
		if (!isset($arg['wid'])) errorHandle::ParameterErrors('site id ');

		$website	= base::init('website')->getRow(array('wid'=>$arg['wid']));
		$attr		= base::init('website')->getConfig($website['domain']);

		if (isset($arg['username']) && isset($arg['password']))
		{
			if (($userinfo	= base::init('member')->login($arg)) !== FALSE)
			{
				$hash	= base::init('member')->cookiehash($arg['username'], get_real_ip(), $_SERVER['HTTP_USER_AGENT'],$website['domain']);
				
				if (!isset($arg['wid'])) errorHandle::ParameterErrors('site id ');
				
				$website	= base::init('website')->getRow(array('wid'=>$arg['wid']));
				$columns	= base::init('column')->getAll(array('wid'=>$arg['wid']));
				$columns	= base::init('column')->listcls($columns,0);
				
				$template	= $this->template();
				$template->template_dir = ROOT . $attr['tempdir'] ;
				$template->assign('attr',$attr);
				$template->assign('columns',$columns);
				$template->assign('url','/');
				$template->assign('stat','登录成功~');
				
				$html	= $template->fetch('refresh.php');
				$json['eval']	= "setcookie(\"user\",\"$hash\",time()+3600*24,\"/\");";
				$json['eval']	.= "setcookie(\"username\",\"".($arg['username'])."\",time()+3600*24,\"/\");";
				$json['eval']	.= "setcookie(\"uid\",\"{$userinfo['id']}\",time()+3600*24,\"/\");";
				$json['eval']	.= "setcookie(\"wid\",\"{$userinfo['wid']}\",time()+3600*24,\"/\");";
				$json['html']	= $html;
				
				$json	= json_encode($json);
				exit($json);
			}
		}	

		$template	= $this->template();
		$message	= 'A record with the supplied identity could not be found or Supplied credential is invalid <br />用户名或密码无效';
		$template->assign('message', $message);
		$template->assign('url', @$arg['referer']);
		$template->display(ROOT . $attr['tempdir'] . 'member_login.php');
		die;
	}

	public function registerAction($arg)
	{
		if (!isset($arg['wid'])) errorHandle::ParameterErrors('site id ');
		$website	= base::init('website')->getRow(array('wid'=>$arg['wid']));
		$attr		= base::init('website')->getConfig($website['domain']);
		$columns	= base::init('column')->getAll(array('wid'=>$arg['wid']));
		$columns	= base::init('column')->listcls($columns,0);
		
		$city		= base::fetchall("select * from my_city");
		$province	= base::fetchall("select * from my_province");
		//$city	= sort_combine($city, "ProvinceID");

		$template	= $this->template();
		$template->template_dir = ROOT . $attr['tempdir'] ;
		$template->assign('attr',$attr);
		$template->assign('city',($city));
		$template->assign('province',$province);
		$template->assign('columns',$columns);

		if (isPost())
		{
			include ROOT . '/libs/Zend/Session/Namespace.php';
			
			$codeSession	= new Zend_Session_Namespace('code');
			var_dump($codeSession->code);exit;
			
			if (!base::init('member')->checkUsername($arg['username']))
			{
				$template->assign('url',$arg['url']);
				$template->assign('stat','用户名已经被使用了~');
				$template->display('refresh.php');
			}
			else 
			{
				$arg_	= $arg;
				$arg_['register_time']	= time();
				$arg_['password']		= md5($arg['password']);
				base::init('member')->insert($arg_);
				$template->assign('url','/');
				$template->assign('stat','注册成功');
				
				$userinfo	= base::init('member')->login($arg);
				$hash	= base::init('member')->cookiehash($arg['username'], get_real_ip(), $_SERVER['HTTP_USER_AGENT'],$website['domain']);
				$html	= $template->fetch('refresh.php');
				$json['eval']	= "setcookie(\"user\",\"$hash\",time()+3600*24,\"/\");";
				$json['eval']	.= "setcookie(\"username\",\"".($arg['username'])."\",time()+3600*24,\"/\");";
				$json['eval']	.= "setcookie(\"uid\",\"{$userinfo['id']}\",time()+3600*24,\"/\");";
				$json['eval']	.= "setcookie(\"wid\",\"{$userinfo['wid']}\",time()+3600*24,\"/\");";
				$json['html']	= $html;
				$json			= json_encode($json);
				exit($json);
			}
		}
		else 
		{
			$template->display('member_register.php');
		}
	}
	
	public function menulistAction($arg)
	{
		if (!isset($arg['wid'])) errorHandle::ParameterErrors('site id ');

		$template	= $this->template();
		$cookie		= _getcookie($arg['cookie']);
		$website	= base::init('website')->getRow(array('wid'=>$arg['wid']));
		$attr		= base::init('website')->getConfig($website['domain']);

		if (isset($cookie['username']))
		{
			$array		= array('username'=>($cookie['username']),'ip'=>get_real_ip(),'useragent'=>$_SERVER['HTTP_USER_AGENT'],'domain'=>$website['domain']);
			$islogin	= @base::init('member')->isLogin($cookie['user'], $array);
		
			$template->assign('islogin',$islogin);
			$template->assign('userinfo',$cookie);
		}
		$template->display(ROOT . $attr['tempdir'] . 'member_menulist.php');
	}

	public function logoutAction($arg)
	{
		$website	= base::init('website')->getRow(array('wid'=>$arg['wid']));
		$attr		= base::init('website')->getConfig($website['domain']);
		$columns	= base::init('column')->getAll(array('wid'=>$arg['wid']));
		$columns	= base::init('column')->listcls($columns,0);

		$template	= $this->template();
		$template->template_dir = ROOT . $attr['tempdir'] ;

		$template->assign('attr',$attr);
		$template->assign('columns',$columns);
		$template->assign('url',"/");
		$template->assign('stat','退出成功~');

		$json['eval']	= "setcookie(\"username\",\"\",time()-3600*24*365,\"/\");";
		$json['html']	= $template->fetch('refresh.php');
		exit(json_encode($json));
	}
}